<template>
	<view>
		<cu-custom bgColor="bg-gradual-red">
			<block slot="content">{{title}}</block>
		</cu-custom>
		<Loading class="loading" v-show="!isLoading" :isStatus='loadStatus'></Loading>
		<basics v-if="PageCur=='basics'" :courseID='courseId'></basics>
		<myself v-if="PageCur=='myself'" ref="changeMyself"></myself>
		<view class="cu-bar tabbar bg-white shadow foot" v-show="isLoading">
			<view class="action" @click="NavChange" data-cur="basics">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/basics' + [PageCur=='basics'?'_cur':''] + '.png'"></image>
				</view>
				<view :class="PageCur=='basics'?'text-green':'text-gray'">首页</view>
			</view>
			<view class="action" @click="NavChange" data-cur="myself">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/about' + [PageCur == 'myself'?'_cur':''] + '.png'"></image>
				</view>
				<view :class="PageCur=='plugin'?'text-green':'text-gray'">我的</view>
			</view>

		</view>
	</view>
</template>
<script>
	import Vue from 'vue'
	export default {
		data() {
			return {
				title: this.CON.CON.title,
				PageCur: 'basics',
				courseId: "",
				isLoading: false,
				loadStatus: 'load',

			}
		},
		onShareAppMessage(res) {
			console.log("分享的openid " + this.initCode);
			return {
				title: "乐知课程艺术中心",
				imageUrl:this.Reques.resDomain +"yishu.png",
				path: '/pages/index/index?userCode=' + this.initCode
			}
		
		},
		onUnload: () => {
			console.log("返回了");
		},
		onLoad(option) {		
			this.Reques.checkLogin((d) => {
				if (!d) {
					uni.getProvider({
						service: 'oauth',
						success: (res) => {
							console.log(res.provider)
							if (~res.provider.indexOf('weixin')) {
								uni.login({
									provider: 'weixin',
									success: (loginRes) => {
										let appid = this.Reques.appid; //appid
										let url = "xcx/weixin.api.php";
										let data = {
											"action": "getWeixinUserInfoByCode",
											"appId": appid,
											"code": loginRes.code
										};
										let tishi = () => {
											uni.showModal({
												title: '提示',
												content: '登录失败',
												showCancel: false,
												success: function(res) {
													if (res.confirm) {
														return false;
														console.log('用户点击确定');
													} else if (res.cancel) {
														console.log('用户点击取消');
													}
												}
											})
										}
		                               //请求接口
										this.Reques.request(url, 'GET', data).then((res) => {
											if (res.statusCode == 200 && res.data.code == 0) {
												Vue.prototype.$coupon=res.data.discount;
												Vue.prototype.$openid = res.data.openId;
												Vue.prototype.initCode=res.data.inviteCode;
												this.isLoading = true;
												uni.setStorage({
													key: "session",
													data: res.data,
													success: () => {
														this.Reques.updataSession();
														console.log("stroage success!");
													},

												});
											} else {
												this.loadStatus = 'error2';
												tishi();
											}

										}, (err) => {
											tishi();
											this.loadStatus = 'error2';
											console.log("获取失败");

										});



									}
								});
							} else {
								uni.showModal({
									title: '提示',
									confirmText: '支持微信小程序'
								});
								console.log("只支持微信");

							}


						}
					});

				} else {
					uni.getStorage({
						key: "session",
						success: (res) => {
							Vue.prototype.$openid = res.data.openId;
							Vue.prototype.initCode=res.data.inviteCode;
							this.isLoading = true;
							this.checkInfo();

						},
						fail: (err) => {
							console.log("信息获取错误" + JSON.stringify(err));

						},

					});

				}

			});
               
		    Vue.prototype.$userCode=(option.userCode??'');
              
			this.courseId = option.courseId;
		},
		methods: {
			/**
			 * 检查信息私服哦发生变化
			 */
			checkInfo(){
			
				let checkLogin=()=>{
					uni.showModal({
						title: "提示",
						content: "用户信息发生变化,是否重新加载",
						success: (res) => {
							if (res.confirm) {
								this.reLogin();
							}
						}
					})
				}
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: (loginRes) => {
									let appid = this.Reques.appid; //appid
									let url = "xcx/weixin.api.php";
									let data = {
										"action": "getWeixinUserInfoByCode",
										"appId": appid,
										"code": loginRes.code
									};
				                   //请求接口
									this.Reques.request(url, 'GET', data).then((res) => {
										if (res.statusCode == 200 && res.data.code == 0) {
											(this.$openid!=res.data.openId)?checkLogin():"";
										} 
									}, (err) => {
										console.log("获取失败");
				
									});
								}
							});
						}
					}
				});	
			},
			
			/**
			 * 回调更新
			 * @param {Object} type
			 */
			
			upData: function(type) {
			
				(this.PageCur=='myself')?(	this.$refs.changeMyself.upadteVue()):"";
				this.$forceUpdate();
			},
			
			NavChange: function(e) {
				this.PageCur = e.currentTarget.dataset.cur;
				if (this.PageCur == "myself") {
					this.courseId="";
					this.title = "个人中心";
				} else {
					this.title = this.CON.CON.title;
				}
			}
		}


	}
</script>
